package com.whs.demo.application;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class TexturedTri implements ApplicationListener {

	public static void main(String[] args) {
//		LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
//		cfg.title = "atmos";
//		cfg.width = 480;
//		cfg.height = 320;
//		new LwjglApplication(new TexturedTri(), cfg);
	}

	BitmapFont font;
	SpriteBatch batch;
	OrthographicCamera camera;
	Texture tex;
	TextureRegion reg;
	float[] vertices;

	@Override
	public void create() {
		font = new BitmapFont();
		batch = new SpriteBatch();
		camera = new OrthographicCamera();
		tex = new Texture(Gdx.files.internal("data/pic/level1.png"));
		reg = new TextureRegion(tex, 50, 0, 100, 90);
		vertices = createTriangle(reg, 2, 2, 10, 10, Color.WHITE);
	}

	protected float[] createTriangle(TextureRegion region, int x, int y,
			int width, int height, Color color) {
		float w = region.getRegionWidth();
		float h = region.getRegionHeight();

		float c = color.toFloatBits();
		float u = region.getU();
		float v = region.getV();
		float u2 = region.getU2();
		float v2 = region.getV2();

		return new float[] { x, y, c, u, v, x, y + h, c, u, v2, x + w, y, c,
				u2, v, x, y, c, u, v };
	}

	@Override
	public void resize(int width, int height) {
		camera.setToOrtho(false, width, height);
		batch.setProjectionMatrix(camera.combined);
	}

	@Override
	public void render() {
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		Gdx.gl.glClearColor(1, 1, 1, 1);
		batch.begin();
		batch.draw(tex, vertices, 0, vertices.length);
		batch.end();
	}

	@Override
	public void pause() {

	}

	@Override
	public void resume() {

	}

	@Override
	public void dispose() {
		font.dispose();
		batch.dispose();
	}
}